﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Jogo_Grupo2.Telas;

namespace Jogo_Grupo2.Elementos
{
    class Player : Objeto2D
    {
       

        float timer = 0;
        float intervalo = 70f;
        int curframe;

        public int spriteWidth;
        public int spriteHeight;

        int velocidade = 2;
        SpriteEffects inverted = SpriteEffects.None;

        KeyboardState lastk, currentk, teclado;

        public int vida;
        public bool morreu;
        public bool imortal;
        public int imortalCounter = 120;

        private bool temBazuca;
        private bool temMetranca;

        enum heroState
        {
            parado,
            atck,
            correr,
            bazuca,
            metranca,
        }

        heroState estadoAtual = heroState.parado;

        public Rectangle spriteRect;
                 
         public List<Tiro> ListaTiros;
         public List<TiroBazuca> ListaBazuca;
         public List<TiroMetranca> ListaMetranca;

        public Player(Texture2D textura, Vector2 position) : base(textura, position)
        {
            curframe = 0;

            vida = 5;

            this.texture = textura;
            spriteWidth = textura.Width / 8;
            spriteHeight = textura.Height / 1;

            spriteRect = new Rectangle(curframe * spriteWidth, curframe * spriteHeight, spriteWidth, spriteHeight);

            ListaTiros = new List<Tiro>();
            ListaBazuca = new List<TiroBazuca>();
            ListaMetranca = new List<TiroMetranca>();

        }

        public override Rectangle collisionBox()
        {
            return new Rectangle((int)position.X, (int)position.Y, spriteWidth, spriteHeight);
        }

        public void trocarAnimacao(Texture2D textura, int framesX, int framesY)
        {
            curframe = 0;

            this.texture = textura;
            spriteWidth = textura.Width / framesX;
            spriteHeight = textura.Height / framesY;

            

            spriteRect = new Rectangle(curframe * spriteWidth, curframe * spriteHeight, spriteWidth, spriteHeight);
        }

        public void perderVida(int dano)
        {
            if (imortal == false)
            {
                vida -= dano;
                imortal = true;

                if (vida <= 0)
                {
                    vida = 0;
                    morreu = true;
                }
            }
        }

        public void verifyImortalidade()
        {
            if (imortal)
            {
                imortalCounter--;

                if (imortalCounter == 0)
                {
                    imortal = false;
                    imortalCounter = 120;
                }
            }
        }
      

        public override void Update(GameTime gameTime)
        {

            correr();
            verifyAnimation(gameTime);
            verifyStates();
            verifyImortalidade();
            atirar();
            AtirarBazuca();
            AtirarMetranca();
            
            
         

            base.Update(gameTime);
            
        }

        public void TiroDisparado()
        {
            if (inverted == SpriteEffects.None)
            {
                Tiro tiro = new Tiro(Game1.instancia.Content.Load<Texture2D>("tiro"), new Vector2(this.position.X + 80, this.position.Y + 45), "direita");
                ListaTiros.Add(tiro);
            }

            else
            {
                Tiro tiro = new Tiro(Game1.instancia.Content.Load<Texture2D>("tiro"), new Vector2(this.position.X -1, this.position.Y + 45), "esquerda");
                ListaTiros.Add(tiro);

            }


        }

        public void BazucaDisparada()
        {
            if (inverted == SpriteEffects.None)
            {
                TiroBazuca tirobazuca = new TiroBazuca(Game1.instancia.Content.Load<Texture2D>("tiro2"), new Vector2(this.position.X + 80, this.position.Y + 45), "direita");
                ListaBazuca.Add(tirobazuca);
            }

            else
            {
                TiroBazuca tirobazuca = new TiroBazuca(Game1.instancia.Content.Load<Texture2D>("tiro2"), new Vector2(this.position.X -1, this.position.Y +45), "esquerda");
                ListaBazuca.Add(tirobazuca);
            }

        }

        public void MetrancaDisparada()
        {
            if (inverted == SpriteEffects.None)
            {
                TiroMetranca tirometranca = new TiroMetranca(Game1.instancia.Content.Load<Texture2D>("tiro"), new Vector2(this.position.X + 80, this.position.Y + 45), "direita");
                ListaMetranca.Add(tirometranca);
            }

            else
            {
                TiroMetranca tirometranca = new TiroMetranca(Game1.instancia.Content.Load<Texture2D>("tiro"), new Vector2(this.position.X -1, this.position.Y + 45), "esquerda");
                ListaMetranca.Add(tirometranca);
            }

        }


        private void verifyAnimation(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (estadoAtual == heroState.parado && curframe >= 7)
            {
                curframe = 0;
            }

           

            if (timer >= intervalo)
            {
                timer = 0;
                curframe++;
            }


            for (int i = 0; i < ListaTiros.Count(); i++)
            {
                ListaTiros[i].Update(gameTime);
            }

            for (int i = 0; i < ListaBazuca.Count(); i++)
            {
                ListaBazuca[i].Update(gameTime);
            }
                
                spriteRect.X = curframe * spriteWidth;

                
                if (estadoAtual == heroState.bazuca)
                {
                    if(curframe % 6 == 0)
                    {
                        int currentHeight = curframe / 6;
                        spriteRect.Y = currentHeight * spriteHeight;
                    }

                    spriteRect.X = (curframe % 6) * spriteWidth;
                }

                
            
            for (int i = 0; i < ListaMetranca.Count(); i++)

                {
                   ListaMetranca[i].Update(gameTime);
                }

                spriteRect.X = curframe * spriteWidth;

                if (estadoAtual == heroState.metranca)
                {
                    if (curframe % 8 == 0)
                    {
                        int currentHeight = curframe / 8;
                        spriteRect.Y = currentHeight * spriteHeight;
                    }

                    spriteRect.X = (curframe % 8) * spriteWidth;
                }
        }

        private void verifyStates()
        {
            if (currentk.IsKeyUp(Keys.Right) && currentk.IsKeyUp(Keys.Left) && estadoAtual != heroState.parado && estadoAtual != heroState.atck && estadoAtual != heroState.bazuca && estadoAtual != heroState.metranca)
            {
                estadoAtual = heroState.parado;
                trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("teste"),8,1);

                if (curframe >= 7)
                {
                    curframe = 0;
                    
                }
            }

            if (estadoAtual == heroState.correr)
            {
                this.position.X += velocidade;    
            }


            if (estadoAtual == heroState.atck)
            {
                //Game1.instancia.TiroDisparado();
                if (teclado.IsKeyUp(Keys.Space))
                {
                    if (curframe >= 7)
                    {
                        curframe = 0;
                        estadoAtual = heroState.parado;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("teste"),8,1);
                    }
                }

            }

            if (estadoAtual == heroState.bazuca && temBazuca)
            {
                
                if (teclado.IsKeyUp(Keys.Z))
                {
                    if (curframe >= 12)
                    {
                        curframe = 0;
                        estadoAtual = heroState.parado;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("teste"),8,1);
                    }
                }
            }

            if (estadoAtual == heroState.metranca)
            {
                if (teclado.IsKeyUp(Keys.C))
                {
                    if (curframe >= 10)
                    {
                        curframe = 0;
                        estadoAtual = heroState.parado;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("teste"), 8, 1);
                    }
                }
            }

        }
        

        public void correr()
        {
            lastk = currentk;
            currentk = Keyboard.GetState();
           

            if (currentk.IsKeyDown(Keys.Right))
                {
                    velocidade = 4;
                    inverted = SpriteEffects.None;

                    if (estadoAtual != heroState.correr && estadoAtual != heroState.atck)
                    {
                        
                        estadoAtual = heroState.correr;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("correr"),8,1);
                    }
                    
                }

            if (currentk.IsKeyDown(Keys.Left))
                {

                    velocidade = -4;
                    inverted = SpriteEffects.FlipHorizontally;

                    if (estadoAtual != heroState.correr && estadoAtual != heroState.atck)
                    {
                        
                        estadoAtual = heroState.correr;
                        trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("correr"),8,1);
                    }
                }
            
        }


       public void atirar()
       {
           if (currentk.IsKeyDown(Keys.Space) && lastk != currentk)   
            {
                if (estadoAtual != heroState.atck)
                    {
                            estadoAtual = heroState.atck;
                            trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("atck"),8,1);
                            TiroDisparado();
                            
                    }
            }

        }

       public void verifyUpgrades(List<PowerUp> lista)
       {
           for (int i = 0; i < lista.Count; i++)
           {
               if (lista[i].powerUp == PowerUp.PowerUpEnum.bazuca)
               {
                   temBazuca = true;
               }

               
           }
       }

       public void AtirarBazuca()
       {
           if (temBazuca)
           {
               if (currentk.IsKeyDown(Keys.Z) && lastk != currentk)
               {
                   if (estadoAtual != heroState.bazuca)
                   {
                       estadoAtual = heroState.bazuca;
                       trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("BAZUCA"), 6, 3);
                       BazucaDisparada();
                   }
               }
           }
       }

       public void AtirarMetranca()
       {
           if (temMetranca)
           {
               if (currentk.IsKeyDown(Keys.C) && lastk != currentk)
               {
                   if (estadoAtual != heroState.metranca)
                   {
                       estadoAtual = heroState.metranca;
                       trocarAnimacao(Game1.instancia.Content.Load<Texture2D>("metranca"), 8, 2);
                       MetrancaDisparada();
                   }
               }
           }
       }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, new Rectangle((int)position.X,(int)position.Y, spriteWidth, spriteHeight), spriteRect, Color.White,0,Vector2.Zero,inverted,0);
            for (int i = 0; i < ListaTiros.Count(); i++)
            {
                ListaTiros[i].Draw(spriteBatch);
            }

            for (int i = 0; i < ListaBazuca.Count(); i++)
            {
                ListaBazuca[i].Draw(spriteBatch);
            }

            for (int i = 0; i < ListaMetranca.Count(); i++)
            {
                ListaMetranca[i].Draw(spriteBatch);
            }
          
        }

    }
}
